package com.qc.controller.web;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.qc.entity.SkillEntity;
import com.qc.entity.SkillEntityVo;
import com.qc.entity.SkillTagEntity;
import com.qc.pageUtil.page.page.Page;
import com.qc.pageUtil.page.page.PageModel;
import com.qc.pageUtil.page.page.PageModelList;
import com.qc.pageUtil.page.vo.NewsAndNav;
import com.qc.serverlistener.CacheUtil;
import com.qc.service.SkillService;

@SuppressWarnings({ "rawtypes", "unchecked" })
@Controller
@RequestMapping("/web")
public class WebSkillController {

	@Resource(name="skillService")
	private SkillService skillService;
	
	/**
     * 跳转所有技能到首页
     * @return
     */
    @RequestMapping(value = "/toSkillIndex", produces = "text/html;charset=UTF-8")
    public ModelAndView toSkillIndex() {
        ModelAndView mav = new ModelAndView("/category/leibie_jineng");
        return mav;
    }
    
    /**
     * 跳转价格计算相关首页
     * @return
     */
    @RequestMapping(value = "/toSkillJineng", produces = "text/html;charset=UTF-8")
    public ModelAndView toSkillJineng() {
        ModelAndView mav = new ModelAndView("/sec/sec_jineng");
        
        List list = skillService.getAllSkillParent();
        mav.addObject("list",list);
        
        return mav;
    }
    
    
    
    /**
     * 所有技能数据展示
     * @param request
     * @return
     */
    @RequestMapping(value = "/toListAllSkill", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String toListAllSkill(HttpServletRequest request) {
        
        String name = request.getParameter("name");
        //当前页面
        int pageNo = Integer.parseInt(request.getParameter("pageNo"));
        //每页显示多少条
        int pageSize = Integer.parseInt(request.getParameter("pageSize"));
        
        //根据条件总数
        int recordCount = skillService.getListSkillCount(name);
        
        //根据条件查询当前页数据
        List<SkillEntity> list = skillService.getAllSkill(name, pageNo, pageSize);
        
        //分页查询公共类
        Page<SkillEntity> page = new Page<SkillEntity>(pageNo, recordCount,pageSize);
        page.setList(list);//设置结果集  
        PageModelList<SkillEntity> pageModel = new PageModelList<SkillEntity>(page, "#", PageModel.NUM_MODEL,"doPage","1");//设置页码显示类型  
        //4 组装带翻页导航功能的对象
        NewsAndNav  objs = new NewsAndNav(list, pageModel.getPageModel());
        
        return JSONObject.fromObject(objs).toString();
        
    }
    
    /**
     * 所有技能添加
     */
    @RequestMapping(value = "/toSaveSkill")
    @ResponseBody
    public int toSaveSkill(HttpServletRequest request) {
        
        String name = request.getParameter("name");
        
        SkillEntity skillEntity = new SkillEntity();
        
        
        skillEntity.setName(name);
        skillEntity.setParent("");
        skillEntity.setLevel("1");
        
        int flag = skillService.saveSkill(skillEntity);
        
        return flag;
    }
    
    /**
     * 所有技能删除
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/toDeleteSkillId")
    @ResponseBody
    public int toDeleteSkillId(HttpServletRequest request,HttpServletResponse response) {
        long id = Long.parseLong(request.getParameter("id"));
        int flag = skillService.deleteSkillId(id);
        return flag;
    }
    
    /**
     * 所有技能修改
     */
    
    @RequestMapping(value = "/toUpdateSkillId")
    @ResponseBody
    public int toUpdateSkillId(HttpServletRequest request, HttpServletResponse response) {
        long id = Long.parseLong(request.getParameter("id"));
        String name = request.getParameter("name");
        
        SkillEntity skillEntity = new SkillEntity();
        
        skillEntity.setName(name);
        skillEntity.setId(id);
        
        int flag = skillService.updateSkillId(skillEntity);
        return flag;
    }
    
    
    /**
     * 所有计算价格技能标签展示
     * @param request
     * @return
     */
    @RequestMapping(value = "/toListAllSecSkill", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String toListAllSecSkill(HttpServletRequest request) {
        
        String name = request.getParameter("name");
        //当前页面
        int pageNo = Integer.parseInt(request.getParameter("pageNo"));
        //每页显示多少条
        int pageSize = Integer.parseInt(request.getParameter("pageSize"));
        
        //根据条件总数
        int recordCount = skillService.getListSecSkillCount(name);
        
        //根据条件查询当前页数据
        List<SkillEntity> list = skillService.getAllSecSkill(name, pageNo, pageSize);
        
        //分页查询公共类
        Page<SkillEntity> page = new Page<SkillEntity>(pageNo, recordCount,pageSize);
        page.setList(list);//设置结果集  
        PageModelList<SkillEntity> pageModel = new PageModelList<SkillEntity>(page, "#", PageModel.NUM_MODEL,"doPage","1");//设置页码显示类型  
        //4 组装带翻页导航功能的对象
        NewsAndNav  objs = new NewsAndNav(list, pageModel.getPageModel());
        
        return JSONObject.fromObject(objs).toString();
        
    }
    
    /**
     * 所有计算价格技能标签展示
     */
    @RequestMapping(value = "/toSaveSecSkill")
    @ResponseBody
    public int toSaveSecSkill(HttpServletRequest request) {
        
    	long categoryId = Long.parseLong(request.getParameter("categoryId"));
        String tagName = request.getParameter("tagName");
        double tagMoney = Double.parseDouble(request.getParameter("tagMoney"));
        String tagContent = request.getParameter("tagContent");
        
        
        SkillEntityVo skillEntityVo = new SkillEntityVo();
        
        
        skillEntityVo.setCategoryId(categoryId);
        skillEntityVo.setTagName(tagName);
        skillEntityVo.setTagMoney(tagMoney);
        skillEntityVo.setTagContent(tagContent);
        
        SkillTagEntity entity = new SkillTagEntity();
        entity.setTagName(tagName);
        entity.setTagMoney(tagMoney);
        entity.setTagContent(tagContent);
        
        CacheUtil.addSkillTag(entity);
        
        int flag = skillService.saveSecSkill(skillEntityVo);
        
        return flag;
    }
    
    /**
     * 所有计算价格技能标签修改
     */
    @RequestMapping(value = "/toUpdateSecSkillId")
    @ResponseBody
    public int toUpdateSecSkillId(HttpServletRequest request, HttpServletResponse response) {
    	
    	long categoryId = Long.parseLong(request.getParameter("categoryId"));
    	long tagId = Long.parseLong(request.getParameter("tagId"));
        String tagName = request.getParameter("tagName");
        double tagMoney = Double.parseDouble(request.getParameter("tagMoney"));
        String tagContent = request.getParameter("tagContent");
        
        SkillEntityVo skillEntityVo = new SkillEntityVo();
        
        skillEntityVo.setTagId(tagId);
        skillEntityVo.setCategoryId(categoryId);
        skillEntityVo.setTagName(tagName);
        skillEntityVo.setTagMoney(tagMoney);
        skillEntityVo.setTagContent(tagContent);
        
        SkillTagEntity entity = new SkillTagEntity();
        entity.setTagName(tagName);
        entity.setTagMoney(tagMoney);
        entity.setTagContent(tagContent);
        
        CacheUtil.addSkillTag(entity);
        
        int flag = skillService.updateSecSkillId(skillEntityVo);
        return flag;
    }
    
    /**
     * 所有计算价格技能标签删除
     */
    @RequestMapping(value = "/toDeleteSecSkillId")
    @ResponseBody
    public int toDeleteSecSkillId(HttpServletRequest request,HttpServletResponse response) {
        long id = Long.parseLong(request.getParameter("id"));
        int flag = skillService.deleteSecSkillId(id);
        return flag;
    }
    
    
	
}
